package com.joolun.script.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joolun.script.dto.CommentExt;
import com.joolun.script.entity.Comment;
import com.joolun.script.mapper.CommentMapper;
import com.joolun.script.service.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 留言内容
 *
 * @author 臣服
 * @date 2022-01-12 10:51:45
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Override
    public List<Comment> queryByParentId(String scriptId, String commentId) {
        LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Comment::getScriptId, scriptId);
        queryWrapper.eq(Comment::getCommentId, commentId);
        queryWrapper.orderByDesc(Comment::getCreateTime, Comment::getId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Comment> getPage(Page<Comment> page, Comment comment) {
        QueryWrapper<Comment> query = Wrappers.query(comment);
        return baseMapper.selectPage(page, query);
    }

    @Override
    public List<CommentExt> queryLastByParentId(String scriptId, List<String> commentIdList, Integer lastNumber) {
        if (StringUtils.isBlank(scriptId) || CollectionUtils.isEmpty(commentIdList)) {
            return Collections.emptyList();
        }
        return baseMapper.queryLastReplyComment(scriptId, commentIdList, lastNumber);
    }

}
